package com.ray.life;

import java.util.ArrayList;
import java.util.List;

import com.google.android.maps.GeoPoint;

import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class NewsAdapter extends BaseAdapter {
    private final String TAG = "NewsAdapter";

    private Context mContext;
    private LayoutInflater mInflater;
    private List<News> mList;

    public NewsAdapter(Context context, List<News> list, boolean isNearBy) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mList = getNewsList(list, isNearBy);
    }

    public int getCount() {
        return mList.size();
    }

    public Object getItem(int position) {
        return mList.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup par) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.news_row, null);
            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.text);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        News tmpN = (News) mList.get(position);
        holder.text.setText(tmpN.getTitle());

        return convertView;
    }

    private class ViewHolder {
        TextView text;
    }

    private List<News> getNewsList(List<News> list, boolean isNearBy) {
        if (!isNearBy) {
            return list;
        }

        Location location = LocationHelper.getLocation((LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE));
        if (location == null) {
            Log.e(TAG, "location is null");
        }
        GeoPoint gpSrc = new GeoPoint((int) (location.getLatitude() * LocationHelper.MILLION), (int) (location.getLongitude() * LocationHelper.MILLION));

        ArrayList<News> al = new ArrayList<News>();
        List<News> tmpLs = new ArrayList<News>();

        String addr = null;
        GeoPoint gpDest = null;
        float[] result = null;

        for (News news : list) {
            String desc = news.getDesc();
            ContentParser cp = new ContentParser(desc);
            cp.parse();

            addr = cp.getActAddr();
            cp = null;

            Log.i(TAG, "addr :" + addr);

            if (TextUtils.isEmpty(addr)) {
                continue;
            }

            gpDest = LocationHelper.getGeoPointByAddress(mContext, addr);
            result = LocationHelper.getDistance(gpSrc, gpDest);

            if ((result == null) || result.length == 0) {
                continue;
            }

            if (result[0] < Constants.NEARBY_DISTANCE) {
                al.add(news);
            } else {
                tmpLs.add(news);
            }
        }
        list.removeAll(tmpLs);

        return al;
    }
}
